package utils;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class YiXinTongSMS {

	public static long readSmsId(String xml) throws Exception {
		SAXBuilder builder = new SAXBuilder();
		// 解析xml，生成Document
		Document doc;
		doc = builder.build(new ByteArrayInputStream(xml.getBytes()));
		// 获得根节点
		Element root = doc.getRootElement();
		// 得到子节点
		Element result = root.getChild("Result");
		Element smsId = root.getChild("SmsId");
		if (result.getText().equals("0")) {
			return Long.parseLong(smsId.getText());
		} else {
			return -1;
		}
	}

	public static long send(String msg, String phone) throws Exception {
		String requestUrl = "http://119.145.9.12/sendSMS.action";
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("enterpriseID", "15922"));
		nameValuePairs.add(new BasicNameValuePair("loginName", "admin"));
		nameValuePairs.add(new BasicNameValuePair("password", MD5Helper
				.getMD5Str("yb1145").toLowerCase()));
		nameValuePairs.add(new BasicNameValuePair("content", msg));
		nameValuePairs.add(new BasicNameValuePair("mobiles", phone));
		String responseString = Http.postByPair(requestUrl, nameValuePairs);
		System.out.println("responseString=================" + responseString);
		return readSmsId(responseString);
	}

	public static void main(String[] args) {
		try {
			System.out
					.println(send(
							"您的EMS快递1135748906803已存入师大北区桂苑16架空层邮宝快递箱7号终端14号箱,取件码:122817.服务号码:18144000235.",
							"13559455537"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
